package com.jnevision.dept.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.jnevision.dept.model.Dept;
import com.jnevision.dept.service.DeptServiceImpl;
import com.jnevision.userdept.model.UserDept;
import com.jnevision.userdept.service.UserDeptServiceImpl;
import com.jnevision.util.DateUtil;

/**
 * <p>
 * 系统功能-部门 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-13
 */
@RestController
@RequestMapping("/depts")
public class DeptController extends ApiController {
    @Autowired
    private DeptServiceImpl deptServiceImpl;
    @Autowired
    private UserDeptServiceImpl userDeptServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody Dept dept) {
        dept.setValid(true);
        return this.success(this.deptServiceImpl.save(dept));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody Dept dept) {
        dept.setUpdateTime(DateUtil.getNormalTime());
        return this.success(this.deptServiceImpl.updateById(dept));
    }

    @DeleteMapping
    public R<Boolean> delete(int id) {
        return this.success(this.deptServiceImpl.removeById(id));
    }

    @GetMapping
    public R<Dept> get(int id) {
        return this.success(this.deptServiceImpl.getById(id));
    }

    /**
     * 根据当前人员所属的部门，获取当前部门及下属的所有部门
     * 
     * @param id
     * @return R<List<Dept>>
     */
    @GetMapping("/children")
    public R<List<Dept>> getAllChildren(int id) {
        return this.success(this.deptServiceImpl.getChildren(id));
    }

    @GetMapping("/parent")
    public R<List<Dept>> getByParentId(int id) {
        return this
            .success(this.deptServiceImpl.list(new QueryWrapper<Dept>().eq("parent_id", id).orderByAsc("order_num")));
    }

    /**
     * 根据人员查询所关联的部门 人必须有部门，如果空指针说明数据不对
     * 
     * @param userId
     * @return
     */
    @GetMapping("/dept")
    public R<Collection<Dept>> getDept(String userId) {
        List<Integer> idList = this.userDeptServiceImpl.list(new QueryWrapper<UserDept>().eq("user_id", userId))
            .stream().map(UserDept::getDeptId).collect(Collectors.toList());

        return this.success(this.deptServiceImpl.listByIds(idList));
    }

    /**
     * 获取当前用户所在的项目部
     * 
     * @param token
     * @return
     */
    @GetMapping("/project")
    public R<Dept> getProject(@RequestHeader(value = "token") String token) {
        Dept dept = this.deptServiceImpl.getProject(token);

        if (dept == null) {
            return this.failed("当前用户不在任何项目部");
        }
        return this.success(dept);
    }

    @GetMapping("/all")
    public List<Dept> listDepts() {
        return this.deptServiceImpl.list();
    }

    /**
     * 更新部门的打卡位置
     * 
     * @param deptId
     * @param position
     * @return
     */
    @GetMapping("/position")
    public R<Boolean> updateLocation(int deptId, String position) {
        try {
            position = URLDecoder.decode(position, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Dept dept = new Dept();
        dept.setId(deptId);
        dept.setPosition(position);

        return this.success(this.deptServiceImpl.updateById(dept));
    }
}
